package com.wxxymaker.model.dao;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.model.entity.Member;
import com.wxxymaker.utils.Const;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;

/**
 * @Author: Soft
 * @Date: 2018/7/13 10:42
 * @Desc:
 */
@Bean
public class MemberDao implements BaseDao<Member> {
    @Override
    public Member add(Member entity) {
        Integer asInt = entity.save().asInt();
        entity.setId(asInt);
        return entity;
    }

    @Override
    public Member delete(int id) {
        Member member = Anima.select().from(Member.class).byId(id);
        if (member != null) {
            member.delete();
            return member;
        }
        return null;
    }

    @Override
    public int change(Member entity) {
        return entity.updateById(entity.getId());
    }

    @Override
    public Member findOne(int id) {
        return Anima.select().from(Member.class).byId(id);
    }

    public Page<Member> like(String name) {
        return Anima.select().from(Member.class)
                .like(Member::getName, name + "%")
                .order("id", OrderBy.DESC)
                .page(1, Integer.MAX_VALUE);
    }

    public Page<Member> page(int pageNo, int size) {
        return Anima.select().from(Member.class).order("id", OrderBy.DESC).page(pageNo, size);
    }

    public Page<Member> page(int pageNo) {
        return this.page(pageNo, Const.longPageSize);
    }

    public long count() {
        return Anima.select().from(Member.class).count();
    }
}
